<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class AuthController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        /* init menu */
        $disco = new Application_Model_DbTable_Administrator();
        $this->view->disco = $disco->getAllDisco();
        
        require_once "models/Db/Event_db.php";
        require_once "models/Utility/Utility.php";
        
        $eventHandle = new Event_db();
        $utility_handle = new Utility();

       $pivaList = $eventHandle->getAllPiva();
        foreach ($pivaList as $pivaList2){
                $piva = $pivaList2['piva'];
                $temp = $eventHandle->getUniqueNamebyPiva($piva);
                $allDisco[$piva] = array ($temp['unique_name'], $temp['name']);
        }

        $allDisco = $utility_handle->subval_sort($allDisco,0);
        $this->view->allDisco = $allDisco;

        foreach ($pivaList as $pivaList2){
            $piva = $pivaList2['piva'];
            $temp = $eventHandle->getPaeseDsicoByPiva($piva);
            $paese[$piva] = $temp['paese'];
        }
        $this->view->paese = array_unique($paese);


        $category = $eventHandle->getAllCategory();
        $this->view->category = $category;
//                print_r($category);

        $zonaList = array('Brescia','Verona','Mantova','Cremona');
        $this->view->zonaList = $zonaList;
    }

    public function indexAction()
    {
        // action body
        $form = new Application_Form_Login();
        $request = $this->getRequest();
        if ($request->isPost()) {
            if ($form->isValid($request->getPost())) {
                // do something here to log in
                if ($this->_process($form->getValues())) {
                    // We're authenticated! Redirect to the home page
                    $urlOptions = array('controller'=>'Admin', 'action'=>'index');
                    $this->_helper->redirector->gotoRoute($urlOptions,'admin');
                }else{

                }
            }
        }
        $this->view->form = $form;
    }

    protected function _process($values)
    {
        // Get our authentication adapter and check credentials
        
                $adapter = $this->_getAuthAdapter();
                
                $adapter->setIdentity($values['username']);
                $adapter->setCredential($values['password']);
        
                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate($adapter);
        
                if ($result->isValid()) {
                    $user = $adapter->getResultRowObject();
                    $auth->getStorage()->write($user);
                    return true;
                }
                return false;
    }

    protected function _getAuthAdapter()
    {
        $administrator = new Application_Model_DbTable_Administrator();
        //$dbAdapter = Zend_Db_Table::getDefaultAdapter();
        $authAdapter = new Zend_Auth_Adapter_DbTable($administrator->getAdapter(),'administrator');


        //$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);

        $authAdapter->setTableName('administrator')
            ->setIdentityColumn('username')
            ->setCredentialColumn('password');
            //->setCredentialTreatment('SHA1(CONCAT(?,salt))');


        return $authAdapter;
    }

    public function logoutAction()
    {
        // action body
        Zend_Auth::getInstance()->clearIdentity();
//        $this->_helper->redirector('index'); // back to login page
        $urlOptions = array('controller'=>'Index', 'action'=>'index');
        $this->_helper->redirector->gotoRoute($urlOptions,'index');
    }


}

?>

